Add AppSettings source generator for IConfigurationBuilder extension#31298
Closed
Add AppSettings source generator for IConfigurationBuilder extension#31298
Conversation
…tion Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
…working Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Can you create a source generator that runs during a build and generates an extension method for IConfigurationBuilder that generates an InMemoryCollection for a appsettings.json file at the root of the project.
So, if i have this faile in my app, the...
Add AppSettings source generator for IConfigurationBuilder extension
Aug 22, 2025
Member
|
/azp run MAUI-public |
|
Azure Pipelines successfully started running 1 pipeline(s). |
Member
|
Cool idea, lets revisit later |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note
Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!
This PR introduces a new source generator that automatically creates an extension method for
IConfigurationBuilderto load configuration values from anappsettings.jsonfile at build time.What does this solve?
Developers often need to include configuration data from JSON files in their MAUI applications. Previously, this required manually reading and parsing JSON files at runtime. This source generator automates the process by generating compile-time code that embeds the configuration values directly.
How it works
When you have an
appsettings.jsonfile in your project root:{ "AppName": "My MAUI App", "Settings": { "EnableLogging": true, "MaxConnections": 100 }, "ConnectionString": "Server=localhost;Database=MyDB", "Features": ["Feature1", "Feature2"] }The source generator automatically creates an extension method that you can use like this:
Generated code
The source generator creates an extension method with hardcoded values from your JSON:
Key features
Settings:EnableLogging,Features:0)appsettings.jsonchangesImplementation details
Microsoft.Maui.Core.ConfigurationSourceGenusingIIncrementalGeneratorMicrosoft.Maui.CorepackageThe source generator integrates seamlessly with the existing MAUI build process and requires no additional setup - just add an
appsettings.jsonfile to your project and usebuilder.Configuration.AddLocalAppSettings().✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.